// pages/center/bind/bind.js
var util = require("../../../utils/util.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tip: "点击获取手机验证码：",
    account: "",
    password:"",
    ImageUri: "",
    GUID: "",
    params: {
      Param: {},
      Action: 105
    },
    params2: {
      Action: "1003-2",
      Param: {
        PhoneNumber: "",
        GUID: "",
        Code: "",
        NumberType: 4
      }
    }

  },

  /**
   * formSubmit 
   */
  formSubmit: function (e) {
    var account = e.detail.value.account;
    var password = e.detail.value.password;
    var basePath = getApp().globalData.basePath.miniAppPath;
    var sessionId = getApp().globalData.sessionId;
    
    wx.request({
      url: basePath+'/wxMa/1/'+account+"/"+password,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "JSESSIONID=" + sessionId
      },
      success:function(res){
          if(res.data.code=="00"){
              //fail to bind
            wx.showToast({
              title: '绑定失败，请退出重试',
            })
          }else if(res.data.code=="01"){
              // success to bind
            wx.showToast({
              title: '绑定成功',
            });
            getApp().globalData.userId = res.data.userId; 
          }else if(res.data.code=="02"){
              // wrong phone
             wx.showToast({
               title: '手机号码无效',
             });
          }else if(res.data.code=="03"){
             // wrong verifycode
             wx.showToast({
               title: '短信验证码无效',
             })
          }
      }
    })
    

  },
  onGetPhoneVerifyCode: function () {
    
    var verifyCode = this.data.verifyCode;
    var params = this.data.params2;
    params.Param.PhoneNumber = this.data.account;
    params.Param.GUID = this.data.GUID;
    params.Param.Code = verifyCode;
    var page = this;
    util.reqMedia(params, function (value) {
      if (value.CodeIsPush == 1) {
        page.setData({
          tip: "重新获取验证码"
        });
      }else if(value.CodeIsPush ==0){
        wx.showToast({
          title: '验证码错误',
        });
      }
    });
  },
  onInputPassword: function (e) {
    this.setData({
      password: e.detail.value
    });
  },
  onInputAccount: function (e) {
    this.setData({
      account: e.detail.value
    });
  },
  onInputVerifyCode: function (e) {
    this.setData({
      verifyCode: e.detail.value
    });
  },
  reloadVerifyCode: function () {
    util.loadVerifyCode(this);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // get verifyCode-pattern from serevr
    util.loadVerifyCode(this);
  },

 
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showLoading({
      title: '稍等',
    })
    wx.reLaunch({
      url: '/pages/center/bind/bind',
    })
    wx.hideLoading();
  },
})